#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
# @Time   : 2022/3/28 15:47
# @Author : 余少琪
"""

import time
from typing import Text
from datetime import datetime
import datetime


def count_milliseconds():
    """
    计算时间
    :return:
    """
    access_start = datetime.now()
    access_end = datetime.now()
    access_delta = (access_end - access_start).seconds * 1000
    return access_delta


def count_time(func):
    def decoration(*args, **kwargs):
        start_time = time.time()
        res = func(*args, **kwargs)
        end_time = time.time()
        # print(f"{func.__name__}:args参数是{args},kwargs参数是{kwargs},执行时间{end_time-start_time}")
        print(f"{func.__name__}:执行时间{end_time - start_time}")
        return res

    return decoration


def count_time_decorator(switch: bool):
    def decoration(func):
        def wrapper(*args, **kwargs):
            start_time = time.perf_counter()
            res = func(*args, **kwargs)
            end_time = time.perf_counter()
            if switch:
                # print(f"{func.__name__}:args参数是{args},kwargs参数是{kwargs},执行时间{end_time-start_time}")
                print(f"{func.__name__}:执行时间{round(end_time - start_time, 2)} s")
            return res

        return wrapper

    return decoration


def timestamp_conversion(time_str: Text) -> int:
    """
    时间戳转换，将日期格式转换成时间戳
    :param time_str: 时间
    :return:
    """

    try:
        datetime_format = datetime.strptime(str(time_str), "%Y-%m-%d %H:%M:%S")
        timestamp = int(
            time.mktime(datetime_format.timetuple()) * 1000.0
            + datetime_format.microsecond / 1000.0
        )
        return timestamp
    except ValueError as exc:
        raise ValueError('日期格式错误, 需要传入得格式为 "%Y-%m-%d %H:%M:%S" ') from exc


def time_conversion(time_num: int):
    """
    时间戳转换成日期
    :param time_num:
    :return:
    """
    if isinstance(time_num, int):
        time_stamp = float(time_num / 1000)
        time_array = time.localtime(time_stamp)
        other_style_time = time.strftime("%Y-%m-%d %H:%M:%S", time_array)
        return other_style_time


def now_time():
    """
    获取当前时间, 日期格式: 2021-12-11 12:39:25
    :return:
    """
    localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
    return localtime


def now_times():
    """
    获取当前时间, 日期格式: 2021-12-11 12:39:25
    :return:
    """
    localtime = time.strftime("%H:%M:%S", time.localtime(time.time()))
    return localtime


def now_time2():
    """
    获取当前时间, 日期格式: 2021-12-11 12:39:25
    :return:
    """
    localtime = time.strftime("%Y%m%d%H%M%S", time.localtime())
    return localtime


def now_time_day():
    """
    获取当前时间, 日期格式: 2021-12-11
    :return:
    """
    localtime = time.strftime("%Y-%m-%d", time.localtime())
    return localtime


def get_time_for_min(minute: int) -> int:
    """
    获取几分钟后的时间戳
    @param minute: 分钟
    @return: N分钟后的时间戳
    """
    return int(time.time() + 60 * minute) * 1000


def get_now_time() -> int:
    """
    获取当前时间戳, 整形
    @return: 当前时间戳
    """
    return int(time.time()) * 1000


def get_future_time(day: int):
    futureTime = (datetime.datetime.now() + datetime.timedelta(days=day)).strftime("%Y-%m-%d %H:%M:%S")
    return futureTime


def get_data_today() -> str:
    return datetime.date.today()


def get_month_first_day():
    now = datetime.date.today()
    return datetime.datetime(now.year, now.month, 1).strftime("%Y-%m-%d")


def get_time_stamp(day=None):
    # 获取当前时间
    current_time = datetime.datetime.now()
    # 计算一天之前的时间
    if day:
        one_day_ago = current_time - datetime.timedelta(days=1)
        # 将一天之前的时间转换为时间戳
        timestamp_one_day_ago = one_day_ago.timestamp()
        return int(timestamp_one_day_ago)
    return int(current_time.timestamp())


if __name__ == '__main__':
    print(get_time_stamp(-1))
